package com.tdtxdcxm.hellowebview.webconfig;

import ohos.agp.components.webengine.*;
import ohos.media.image.PixelMap;
import ohos.utils.net.Uri;

public class SetWebAgent extends WebAgent {

    public String URL;

    @Override
    public void onLoadingPage(WebView webview, String url, PixelMap icon) {
        super.onLoadingPage(webview, url, icon);

        System.out.println("网页未知阶段！");
    }
    @Override
    public void onPageLoaded(WebView webview, String url) {

        System.out.println("网页已关闭或切换！");
    }
    @Override
    public void onLoadingContent(WebView webview, String url) {

        System.out.println("网页展示中！");
    }

    @Override
    public void onError(WebView webview, ResourceRequest request, ResourceError error) {
        super.onError(webview, request, error);
    }

    @Override
    public ResourceResponse processResourceRequest(WebView webView, ResourceRequest request) {
        return super.processResourceRequest(webView,request);
    }


    @Override
    public boolean isNeedLoadUrl(WebView webview, ResourceRequest request) {

        if (request == null || request.getRequestUrl() == null) {
            return false;
        }
        Uri uri = request.getRequestUrl();

        if (uri.getDecodedHost().equals(URL)) {

            return false;
        }

        String url = request.getRequestUrl().toString();
        if ( url.startsWith("http:") || url.startsWith("https:") ) {

            return true;
        } else {
            return false;
        }

    }

    public SetWebAgent(String URL) {
        this.URL = URL;
    }
}
